import time

from selenium import webdriver
from selenium.webdriver.common.by import By
# 键盘操作
from selenium.webdriver.common.keys import Keys
# 引入ActionChains类 鼠标操作
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support.wait import WebDriverWait
from handle_excel import HandleExcel


def get_zhibodapan_data():
    driver = webdriver.Chrome()  # 实例化谷歌浏览器
    driver.maximize_window()  # 最大化浏览器
    driver.get('http://livedashboard.corp.hongsong.club/liveRoomBoard')  # 打开直播大盘
    driver.implicitly_wait(10)  # 设置10秒隐式等待

    hde = HandleExcel()  # 实例化excel处理工具类
    roomIds = hde.get_columns_value("B")[1::]  # 获取excel中的roomId
    count = 0
    blacklist = []  # 初始化黑名单
    for roomId in roomIds:
        if_continue = None  # 初始化控制结束本次循环的值
        if roomId is not None:
            print(roomId)
            # 输入roomId，进行搜索操作
            WebDriverWait(driver, 10).until(lambda x: x.find_element(By.ID, 'room_id')).send_keys(roomId)
            driver.find_element(By.CSS_SELECTOR, '[class="ant-btn css-k7429z ant-btn-primary"]').click()
            time.sleep(3)
            # 点击搜索到的数据
            driver.find_element(By.CSS_SELECTOR, '[class="options___xBcfl"]').click()
            time.sleep(5)
            # 点击空格，模拟滑动页面操作
            for i in range(5):
                driver.find_element(By.LINK_TEXT, '直播间质量').send_keys(Keys.SPACE)
                time.sleep(2)

            # 获取['直播到课人数', '总卡顿秒数', '持续卡顿秒数']字段
            list_value_int = ['直播到课人数', '总卡顿秒数', '持续卡顿秒数', '10s以下卡顿用户数', '10-50s卡顿用户数',
                              '50s以上卡顿用户数', '3次以上卡顿用户数']
            list_live_data = []
            for i in list_value_int:
                var = WebDriverWait(driver, 1000).until(
                    lambda x: x.find_element(By.XPATH,
                                             '//*[text()="%s"]/..//*[@class="ant-statistic-content-value-int"]' % i)).text
                if i in ['10s以下卡顿用户数', '10-50s卡顿用户数', '50s以上卡顿用户数',
                         '3次以上卡顿用户数']:  # 判断为这几个字段，则进行百分比换算
                    var = var.replace(',', '')  # 将数量中的"，"去掉
                    live_people_number = list_live_data[0]  # 取出直播列表中的直播到课人数
                    live_people_number = live_people_number.replace(',', '')  # 将数量中的"，"去掉
                    if int(live_people_number) == 0:  # 如果直播到课人数为0，则给回打印，退出本次循环
                        print(f'该场次{roomId}直播到课人数为0')
                        blacklist.append(roomId)
                        driver.back()  # 页面回退
                        if_continue = True
                        break
                    else:  # 不为0，则计算百分比，往下执行
                        var = '%0.2f%%' % (int(var) / int(live_people_number) * 100)  # 计算得到百分比，四舍五入
                list_live_data.append(var)
            if if_continue:  # 如果条件成立，则跳过本次外层循环
                count += 1
                continue
            # 获取['直播平均看课时长', '总卡顿率', '持续卡顿占比']字段
            list_value = ['直播平均看课时长', '总卡顿率', '持续卡顿占比']
            for i in list_value:
                var = WebDriverWait(driver, 1000).until(
                    lambda x: x.find_element(By.XPATH,
                                             '//*[text()="%s"]/..//*[@class="ant-statistic-content-value"]' % i)).text
                list_live_data.append(var)

            print(list_live_data)

            # 获取对应roomId所位于的行号
            row_num = hde.get_rows_number(roomId, 'B')
            hde.excel_write_data(row_num, 5, list_live_data[0])  # 写入直播到课人数到excel
            hde.excel_write_data(row_num, 8, list_live_data[1])  # 写入总卡顿秒数到excel
            hde.excel_write_data(row_num, 9, list_live_data[2])  # 写入持续卡顿秒数到excel
            hde.excel_write_data(row_num, 12, list_live_data[3])  # 写入10s以下卡顿率到excel
            hde.excel_write_data(row_num, 13, list_live_data[4])  # 写入10-50s卡顿率到excel
            hde.excel_write_data(row_num, 14, list_live_data[5])  # 写入50s以上卡顿率到excel
            hde.excel_write_data(row_num, 15, list_live_data[6])  # 写入3次以上卡顿率到excel
            hde.excel_write_data(row_num, 6, list_live_data[7])  # 写入直播平均看课时长到excel
            hde.excel_write_data(row_num, 10, list_live_data[8])  # 写入总卡顿率到excel
            hde.excel_write_data(row_num, 11, list_live_data[9])  # 写入持续卡顿占比到excel

            count += 1
            print(f'第{count}条数据处理完成')
            driver.back()  # 一条数据处理完毕，页面回退

    print(blacklist)
    return count  # 返回总数
